﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Z1029.PhiPlayer.OpenSource
{
    public class PauseGame : MonoBehaviour
    {
        public GameObject pauseUI;
        public GameObject resumeTimer;
        public Animator gameUiAnim;
        public Image pauseRing;
        public static bool pause;
        public static bool start;
        bool clickedPause;

        private void Start()
        {
            pause = true;
            start = false;
            LevelController.instance.m_level.localScale = new Vector3(0f, 1f, 1f);
            StartCoroutine(WaitStartGame());
            StartCoroutine(StartLevel());
        }

        private void OnApplicationPause(bool focus)
        {
            if (start && !pause) Pause();
        }

        private void OnApplicationFocus(bool focus)
        {
            if (start && !pause) Pause();
        }

        IEnumerator WaitStartGame()
        {
            double start = AudioSettings.dspTime;
            bool playedAudio = false;
            yield return null;
            for (; ; )
            {
                if (!playedAudio && AudioSettings.dspTime > start + 1.5)
                {
                    playedAudio = true;
                    PlayMusic();
                }
                if (AudioSettings.dspTime > start + 3.0)
                {
                    StartGame();
                    break;
                }
                yield return null;
            }
        }

        private void StartGame()
        {
            LevelController.instance.StartSyncMusicTime();
            start = true;
            pause = false;
        }

        private void PlayMusic()
        {
            LevelController.instance.musicSource.PlayScheduled(AudioSettings.dspTime + 1.5 + LevelController.musicOffset);
        }

        public void Pause()
        {
            if (start)
            {
                LevelController.instance.musicSource.Pause();
                LevelController.instance.progressControl.PauseAudio();
                pause = true;
                pauseUI.SetActive(true);
            }
        }

        public void ClickPauseButton()
        {
            if (clickedPause)
            {
                Pause();
                clickedPause = false;
            }
            else
            {
                clickedPause = true;
                StartCoroutine(ClickPause());
            }
        }

        IEnumerator ClickPause()
        {
            float timer = 0;
            for (; ; )
            {
                timer += Time.smoothDeltaTime;
                pauseRing.color = new Color(1, 1, 1, timer);
                if (timer > 0.5f) break;
                yield return null;
            }
            clickedPause = false;
            pauseRing.color = (new Color(255, 255, 255, 1)) / 255;
            yield return null;
        }

        public void ClickContinueButton()
        {
            Destroy(Instantiate(resumeTimer, Vector3.zero, Quaternion.identity), 3);
            StartCoroutine(ClickResume());
        }

        IEnumerator ClickResume()
        {
            //LevelController.instance.musicSource.PlayScheduled(AudioSettings.dspTime + 3.0 + LevelController.musicOffset);
            //3秒后继续
            double start = AudioSettings.dspTime;
            for (; ; )
            {
                if (AudioSettings.dspTime > start + 3.0)
                {
                    Continue();
                    break;
                }
                yield return null;
            }
        }

        public void Continue()
        {
            LevelController.instance.musicSource.Play();
            LevelController.instance.progressControl.ResumeAudio();
            PauseGame.pause = false;
            pauseUI.SetActive(false);
        }

        public void ReStart()
        {
            Application.LoadLevel("GamePlay");
        }

        public void ClickBack()
        {
            StartCoroutine(Back());
        }

        IEnumerator Back()
        {
            gameUiAnim.SetTrigger("ExitLevel");
            yield return new WaitForSeconds(1);
            Application.LoadLevelAsync("Menu");
        }

        public void End()
        {
            gameUiAnim.SetTrigger("EndLevel");
            StartCoroutine(EndLevel());
        }

        IEnumerator StartLevel()
        {
            for (; ; )
            {
                if (!start)
                {
                    if (LevelController.instance.m_level.localScale.x < 1f)
                    {
                        LevelController.instance.m_level.localScale = new Vector3(LevelController.instance.m_level.localScale.x + Time.deltaTime * 0.8f, 1f, 1f);
                    }
                }
                else
                {
                    LevelController.instance.m_level.localScale = Vector3.one;
                    break;
                }
                yield return null;
            }
            yield return null;
        }

        IEnumerator EndLevel()
        {
            for (; ; )
            {
                if (LevelController.instance.m_level.localScale.x > 0f)
                {
                    LevelController.instance.m_level.localScale = new Vector3(LevelController.instance.m_level.localScale.x - Time.deltaTime * 0.9f, 1f, 1f);
                }
                else
                {
                    LevelController.instance.m_level.localScale = Vector3.zero;
                    break;
                }
                yield return null;
            }
            yield return null;
        }
    }
}